"use strict";

function md5FromFile() {
    console.log("\nmd5FromFile");

    // 小文件提取 md5摘要
    let fs = require("fs");
    let buf = fs.readFileSync("txtTest.txt");

    let crypto = require('crypto');
    let hash = crypto.createHash('md5');
    hash.update(buf, 'utf8');
    let fileMd5 = hash.digest('hex');

    console.log("txtTest.txt => " + buf.toString());
    console.log("txtTest.txt ==md5==> " + fileMd5);
}



function md5FromStr(oStr) {
    //console.log("\nmd5FromStr");
    // 字符串 提取 md5 摘要

    let strBuf = Buffer.from(oStr, 'utf-8');
    let crypto = require('crypto');
    let md5Maker = crypto.createHash('md5');
    md5Maker.update(strBuf, 'utf8');
    let strMd5 = md5Maker.digest('hex');

    //console.log("oStr => " + oStr);
    //console.log("oStr ==md5==> " + strMd5);

    return strMd5;
}

function md5Demo() {
    md5FromFile();

    console.log("\nmd5FromStr");
    let oStr = "\
七律·长征 \n\
毛泽东 \n\
1935年10月 \n\
红军不怕远征难，万水千山只等闲。 \n\
五岭逶迤腾细浪，乌蒙磅礴走泥丸。 \n\
金沙水拍云崖暖，大渡桥横铁索寒。 \n\
更喜岷山千里雪，三军过后尽开颜。";
    let strMd5 = md5FromStr(oStr);

    console.log("oStr => " + oStr);
    console.log("oStr ==md5==> " + strMd5);
}

function urlEncodeDecode() {
    console.log("\nurlEncodeDecode");

    // https://www.jianshu.com/p/8a48c96ed36c

    let oStr = "https://www.baidu.com/s?tn=50000021_hao_pg&ie=utf-8&word=swift如何做urlencode site:jianshu.com";
    
    let encode = encodeURI(oStr);
    
    let decodeStr = decodeURI(encode);

    console.log (oStr);
    console.log (encode);
    console.log (decodeStr);
}

function base64Demo() {
    console.log("\nbase64Demo");

    let oStr = "\
七律·长征 \n\
毛泽东 \n\
1935年10月 \n\
红军不怕远征难，万水千山只等闲。 \n\
五岭逶迤腾细浪，乌蒙磅礴走泥丸。 \n\
金沙水拍云崖暖，大渡桥横铁索寒。 \n\
更喜岷山千里雪，三军过后尽开颜。";

    // https://blog.csdn.net/gulang03/article/details/88854858
    //JS 标准API atob 与 btoa 仅支持 ASCII 字符串，不能处理中文等
    // Buffer 是 Nodejs 中的标准全局对象（即无需导入），可以拿来使用。
    let base64Str = Buffer.from(oStr, 'utf-8').toString('base64');

    let decodeStr = Buffer.from(base64Str, 'base64').toString('utf-8');

    console.log(oStr);
    console.log(base64Str);
    console.log(decodeStr);
}

function aesEncrypt(ostr, psw) {
    let passwordMd5 = md5FromStr(psw);
    let key = Buffer.from(passwordMd5, "hex");
    //console.log("key " + key);
    let iv = Buffer.from("ddffc44a93503156abb36e9bbca876f8", "hex");
    //console.log("iv " + iv);

    let crypto = require('crypto');
    let cipher = crypto.createCipheriv('AES-128-CBC', key, iv);

    let aesText = cipher.update(ostr, "utf-8", "hex");
    aesText += cipher.final("hex");

    return aesText;
}

function aesDEcrypt(estr, psw) {
    let passwordMd5 = md5FromStr(psw);
    let key = Buffer.from(passwordMd5, "hex");
    //console.log("key " + key);
    let iv = Buffer.from("ddffc44a93503156abb36e9bbca876f8", "hex");
    //console.log("iv " + iv);

    let crypto = require('crypto');
    let decipher = crypto.createDecipheriv('AES-128-CBC', key, iv);

    let dText = decipher.update(estr, "hex", "utf-8");
    dText += decipher.final("utf8");

    return dText;
}

function aesDemo() {
    console.log("\naesDemo");
    // AES 加密
    let fs = require("fs");
    let buf = fs.readFileSync("txtTest.txt");

    let aesText = aesEncrypt(buf.toString(), 'testAESPassword');

    console.log("txtTest.txt => " + buf.toString());
    console.log("txtTest.txt == password + AES-128-CBC==> " + aesText);

    // AES 解密
    let dText = aesDEcrypt(aesText, 'testAESPassword');

    console.log("密文 == password + AES-128-CBC==> " + dText);
}

function demoLogic() {
    console.log("\ncryptoDemoLogic");
    base64Demo();

    urlEncodeDecode();

    md5Demo();

    aesDemo();
}

//export { demoLogic };
module.exports.demoLogic = demoLogic;